package org.massdgames.thunder.server.domain;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.massdgames.thunder.server.domain.strategy.FetchStrategy;
import org.massdgames.thunder.server.domain.strategy.LoadStrategy;

import com.google.inject.Inject;

public abstract class Deck {
    
    private ArrayList<Card> cardList = new ArrayList<Card>();
    private Random randomizer = new Random();
    
    protected FetchStrategy fetchStrategy;
    protected LoadStrategy loadStrategy;
    
    public Deck() {}
    
    @Inject
    public Deck(FetchStrategy fetchStrategy, LoadStrategy loadStrategy) {
        this.fetchStrategy = fetchStrategy;
        this.loadStrategy = loadStrategy;
    }
    
    public void addCard(Card card) {
        this.cardList.add(card);
    }
    
    public Card getRandomCard() throws EmptyDeckException {
        if (cardList.size() < 1) {
            throw new EmptyDeckException("Deck is empty");
        }
        else {
            int index = randomizer.nextInt(cardList.size());
            return cardList.remove(index);
        }
    }

    public int getSize() {
        return cardList.size();
    }

    public ArrayList<Card> getCards() throws FetchException {
        return fetchStrategy.getCards(this);
    }

    public void load() throws IOException {
        loadStrategy.load(this);
    }
    
}
